<?php
	if(!MB_admin){
		exit();
	}
	/*
	$recomm = new MBAdminRecomm($host,$root,$pwd,$db);
	$rets = $recomm->showrUser($p,10);
	$mods = $recomm->getMod(3);
	$modname = $mods['list'][0]['name'];
	$pnum = $mods['list'][0]['p2'];
	$thishost = $_SERVER['HTTP_HOST'];*/
?>
<style type="text/css">
h2{font-size:14px;margin:0 0 10px 20px;padding:0;}
h2 em{font-weight:normal;font-style:normal;font-size:12px;vertical-align:middle;}
h2 strong{margin:0 0 0 -20px;vertical-align:middle;}
h2 em,h2 strong{float:left;}
h2 span{float:right;}	
h3{font-size:12px;margin:0;padding:0;font-family:verdana;color:#f90;}
textarea{color:#000;}
.hotplayinglist{margin:0 0 0 20px;}
.hotplayinglist li{margin:0 0 10px;padding:7px;border:1px solid #aaf;}
.hotplayinglist li.disabled{border-color:#eee;}
.hotplayinglist li div{margin:5px 0 0;}
#addform{display:none;}
</style>
<div class="mainB">
	<?php
function getContentStr($_arr)
{	if(!is_array($_arr)){return $_arr;}
	$s="";
	foreach($_arr as $d)
	{$s.="<a href=\"".$d["link"]."\" target=\"_blank\"".($d["color"]?" style=\"color:".$d["color"]."\"":"").">".$d["text"]."</a>\n";}
	return $s;
}	

function getHotPlayingList($arr,$type)
{	if(!is_array($arr)){return $arr;}
	$str="";
	foreach($arr as $d)
	{
		$str.="<li".($d["state"]==0?" class=\"disabled\"":"")."><h3>".$d["time"]." <span><a href=\"?cid=9&id=".$d["id"]."&t=".$type."\">修改</a></span></h3><div>".getContentStr($d["content"])."</div></li>";
	}
	$str="<ul class=\"hotplayinglist\">".$str."</ul>";
	return $str;
}

	if(!isset($_GET["id"]))
	{
		@require_once MB_ADMIN_DIR."/data/hotplaying.php";
		$dayarr=$hotplaying["day"];
		$weekarr=$hotplaying["week"];
		echo "<h2>历史天记录<a href=\"?cid=9&id=0&t=d\">添加记录</a></h2>".getHotPlayingList($dayarr,"d");
		echo "<h2>历史周记录<a href=\"?cid=9&id=0&t=w\">添加记录</a></h2>".getHotPlayingList($weekarr,"w");
	}
	else{
		$id=intval($_GET["id"]);
		$type=isset($_GET["t"])&&$_GET["t"]=="w"?"week":"day";
		if($id==0)
		{	$m="add";
			$time=date("Y-m-d");
			$content="";
			$state=1;
		}
		else
		{
			require_once "data/hotplaying.php";
			if(isset($hotplaying))
			{	
				$m="edit";
				$d=$hotplaying[$type];
				foreach($d as $c)
				{	
					if($id==$c["id"]){
					$time=$c["time"];
					$contentArr=$c["content"];
					$state=$c["state"];
					//print_r($c);
					break;
					}
				}
				$content="";
				foreach($contentArr as $d)
				{	if($d["text"]&&$d["text"])
					{
					$content.="".$d["text"].",".$d["link"];
					if($d["color"])
					{$content.=",".$d["color"];}
					$content.="\n";
					}
				}
			}
		}
	?>
	<form id="modForm" name="modform" method="post" action="admin_recomm_act.php?a=hotplaying&m=<?php echo $m;?>" class="table_header">
		<h2>
		<strong><?php echo($type=="week"?"本周":"今天");?> </strong>
		<em>
		<input type="text" name="time" value="<?php echo $time;?>"/>
		<input type="hidden" value="<?php echo($id);?>" name="id"/>
		<input type="hidden" value="<?php echo($type);?>" name="type"/>
		启用<input type="checkbox" value="1" name="state"<?php echo($state==1?" checked":"");?>/>
		</em>
		<span><a href="?cid=9">返回列表</a></span>
		</h2>
		<textarea style="width:90%;height:440px;" name="content" placeholder="链接名称,链接地址(,颜色)"><?php echo($content);?></textarea>
		<div id="addform">链接名称：<input type="text" size="15" name="add_name"/> 链接地址：<input type="text" size="15" placeholder="http://" name="add_link"/> 颜色：<input type="text" size="8" placeholder="颜色" name="add_color"/><input type="button" value="添加"/></div>
		<div style="color:gray;margin:10px 0 20px;">格式：链接名称,链接地址,颜色(可不写)，多个链接请换行,<a href="javascript:void(0)" id="toggleAddForm">点击此处添加</a></div>
		
		<div><input type="submit" value="提交保存"/></div>
	</form>
	<script>
		$(document).ready(function(){
		$("#toggleAddForm").click(function(){$("#addform").toggle("slow");});
		$("#addform").find("input[type='button']").click(function(){
			var f=$("#addform"),f1=f.find("input[name='add_name']"),f2=f.find("input[name='add_link']"),f3=f.find("input[name='add_color']");
			if(f1.val()!=""&&f2.val()!="")
			{
				var str=f1.val()+","+f2.val();
				if(f3.val()!="")
				{str+=","+f3.val();}
				str+="\n";
				$("textarea").val($("textarea").val()+str);
			}
			else
			{alert("链接名称和链接地址都不能为空！");}
		});
		});	
	</script>
	<?php }?>
</div>
